﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour
{
		public Armor equipedArmor;  //armadura equipada.
		public Gun equipedGun;  //Arma equipada.
		public int maxItems;    //maxima cantidad de items en el inventario.
		int selectedItem; //indicedel item seleccionado del inventario.
		bool selected;  //Bool que activa o desactiva la accion a hacer con el item(use, delete);
		Stats stats;    //Referencia a los stats del personaje.
		List<Item> itemList = new List<Item> (); //inventario del personaje(no equipads);

		GUIStyle style = new GUIStyle ();
		public Texture Attack, Cancel;
		Vector2 screenPos;
		public Rect windowRect = new Rect (20, 20, 100, 200);

		void DoMyWindow (int windowID)
		{
				if (selected) {
						switch (itemList [selectedItem].type) {
						case Item.TypeOfItem.Gun:
								if (GUI.Button (new Rect (0, 0, 100, 100), "Equipe")) {
										itemList.Add (equipedGun);
										itemList [selectedItem].Use (transform.position);
										equipedGun = (Gun)itemList [selectedItem];
										stats.CalculateStats ();
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								if (GUI.Button (new Rect (50, 100, 100, 100), "Delete")) {
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								break;
						case Item.TypeOfItem.Consumible:
								if (GUI.Button (new Rect (0, 0, 100, 100), "Use")) {
										stats.Heal (itemList [selectedItem].Use (transform.position));
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								if (GUI.Button (new Rect (50, 100, 100, 100), "Delete")) {
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								break;
						case Item.TypeOfItem.Equipable:
								if (GUI.Button (new Rect (0, 0, 100, 100), "Equipe")) {
										itemList.Add (equipedArmor);
										itemList [selectedItem].Use (transform.position);
										equipedArmor = (Armor)itemList [selectedItem];
										stats.CalculateStats ();
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								if (GUI.Button (new Rect (50, 100, 100, 100), "Delete")) {
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								break;
						case Item.TypeOfItem.DefaultGun:
								if (GUI.Button (new Rect (0, 0, 100, 100), "Equipe")) {
										itemList.Add (equipedGun);
										itemList [selectedItem].Use (transform.position);
										equipedGun = (Gun)itemList [selectedItem];
										stats.CalculateStats ();
										itemList.RemoveAt (selectedItem);
										selected = false;
										break;
								}
								break;
						default:
								Debug.Log ("Unknown type");
								break;
						}
				}
		}

		void Start ()
		{  
				stats = gameObject.GetComponent<Stats> ();
				stats.CalculateStats ();
		}

		void OnGUI ()
		{
				//dibujado popup action
				if (selected) {
						windowRect = GUI.Window (0, new Rect (screenPos.x - 75, screenPos.y - 100, 150, 200), DoMyWindow, "", style);
				}
				//fin popup action

				//Dibujado items inventario.

				//equipedGun
				GUI.DrawTexture (new Rect (10, Screen.height / 2, (Screen.height / 7), (Screen.height / 7)), equipedGun.Image);
				GUI.Box (new Rect (10, Screen.height / 2, (Screen.height / 7), (Screen.height / 7)), "" + equipedGun.type);
				//equipedGun
				//equipedArmor
				GUI.DrawTexture (new Rect (10, (Screen.height / 2) + (Screen.height / 7), (Screen.height / 7), (Screen.height / 7)), equipedArmor.Image);
				GUI.Box (new Rect (10, (Screen.height / 2) + (Screen.height / 7), (Screen.height / 7), (Screen.height / 7)), "" + equipedArmor.type);
				//equipedArmor

				//Dubujo stats
				GUI.Box (new Rect (10, (Screen.height / 2) - (Screen.height / 7), (Screen.height / 7), (Screen.height / 7)),
            "Stats\n" +
						"HP: " +
						stats.Max_HP + "\n" +
						"Armor: " + stats.Armor + "\n" +
						"Damage: " +
						stats.Damage + "\n" +
						"Ammo: " + equipedGun.Ammmo
				);
				//Fin dibujo stats

				//Dubuja items en el inventario.
				GUI.Box (new Rect ((Screen.height / 7) - 10, Screen.height - ((Screen.height / 7) + 20), (Screen.height / 7) * maxItems/*itemList.Count*/ + 20, (Screen.height / 7) + 20), "");
				for (int i = 0; i < itemList.Count; i++) {
						GUI.DrawTexture (new Rect ((Screen.height / 7) * (i + 1), Screen.height - (Screen.height / 7), (Screen.height / 7), (Screen.height / 7)), itemList [i].Image);
						if (GUI.Button (new Rect ((Screen.height / 7) * (i + 1), Screen.height - (Screen.height / 7), (Screen.height / 7), (Screen.height / 7)), "" + itemList [i].type)) {
								selectedItem = i;
								selected = true;
								screenPos = new Vector2 (Input.mousePosition.x, Screen.height - Input.mousePosition.y);
						}
				}
				//Fin dibujo items inventario.
		}

		bool InventoryFull ()
		{
				if (itemList.Count < maxItems) {
						return false;
				} else {
						return true;
				}
		}

		public bool AddItem (Item it)
		{
				if (!InventoryFull ()) {
						itemList.Add (it);
						return true;
				} else {
						return false;
				}
		}
}
